package com.example.sinoyd.frameapplication.KotlinFrame.Adapter

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.GVkeyvalue
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.dip2px
import com.example.sinoyd.frameapplication.R
import kotlin.collections.ArrayList

/**
 * 作者： scj
 * 创建时间： 2018/1/15
 * 版权： 二维列表中，中间的girdview适配器
 * 描述： com.sinoyd.Code.Adapter
 */

class MiddleAdapter(var list: ArrayList<GVkeyvalue>, var context: Context, var eachwidth: Float) : BaseAdapter() {
    var paramsonlyone = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

    override fun getView(p0: Int, convertView: View?, p2: ViewGroup?): View {
        var view: View
        var holder: ViewHolder?
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.frame_item_middle_layout, null)
            holder = ViewHolder(view)
            view.tag = holder
        } else {
            view = convertView
            holder = view.tag as ViewHolder
        }


        holder.name!!.text = list[p0].kv


        when (list[p0].kv) {
            "在线" -> {
                //创建一个被选中的背景
                holder.rl!!.removeAllViews()
                val gard = GradientDrawable()
                gard.setColor(Color.parseColor("#00ff00"))
                gard.setStroke(2, Color.parseColor("#00ff00"))
                gard.cornerRadii = floatArrayOf(100f, 100f, 100f, 100f, 100f, 100f, 100f, 100f)
                holder.name!!.setBackgroundDrawable(gard)
                paramsonlyone.setMargins(10, 10, 10, 10)
                holder.rl!!.addView(holder.name!!, paramsonlyone)
            }
            "离线" -> {
                //创建一个被选中的背景
                holder.rl!!.removeAllViews()
                val gard = GradientDrawable()
                gard.setColor(Color.parseColor("#ff0000"))
                gard.setStroke(2, Color.parseColor("#ff0000"))
                gard.cornerRadii = floatArrayOf(100f, 100f, 100f, 100f, 100f, 100f, 100f, 100f)
                holder.name!!.setBackgroundDrawable(gard)
                paramsonlyone.setMargins(10, 10, 10, 10)
                holder.rl!!.addView(holder.name!!, paramsonlyone)
            }
        }


        //变更每一个控件的宽度
        val lp: LinearLayout.LayoutParams = holder.rl!!.layoutParams as LinearLayout.LayoutParams
        lp.width = dip2px(context, eachwidth)
        holder.rl!!.layoutParams = lp



        return view

    }

    override fun getItem(p0: Int): Any = list[p0]

    override fun getItemId(p0: Int): Long = 0L

    override fun getCount(): Int = list.size


    internal inner class ViewHolder(view: View) {
        var name: TextView? = null
        var rl: RelativeLayout? = null

        init {
            name = view.findViewById(R.id.middlename)
            rl = view.findViewById(R.id.rlmiddlename)
        }
    }
}